package com.jdz.gxq.info.admin.registration.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import com.jdz.gxq.info.admin.registration.bo.Registration;
import com.jdz.gxq.info.admin.registration.form.EditRegistrationForm;
import com.jdz.gxq.info.admin.registration.service.RegistrationService;
import com.jdz.gxq.info.common.action.CommonAction;

public class SaveRegistrationAction extends CommonAction {

	private RegistrationService registrationService = null;
	 
	public ActionForward doExecute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws java.lang.Exception {
		
		EditRegistrationForm editRegistrationForm = (EditRegistrationForm) form;
		
		Registration registration = editRegistrationForm.getRegistration();
		
		// registration名重复验证(其他后台逻辑验证也类似处理)
		if (!registrationService.checkRegistrationName(registration, 2)) {
			ActionMessages messages = new ActionMessages();
			messages.add("registration.enterpriseName", new ActionMessage("error.registrationNameExisted"));
			addErrors(request, messages);
			return mapping.getInputForward();
		}

		registration.setModifiedBy(getUserId(request));
		
		registrationService.updateRegistration(registration);
		
		return mapping.findForward("success");
	}

	public void setRegistrationService(RegistrationService registrationService) {
		this.registrationService = registrationService;
	}
}

